package com.loadmore.common.net;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.loadmore.common.net.impl.RequestCallBack;
import com.loadmore.common.util.LogUtil;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author by sunleilei
 * @date 2017/9/26
 * @time 下午8:12
 */

public class RequestUtil {

    private static RequestUtil mInstance;
    private Context context;

    private RequestUtil(Context context) {
        this.context = context;
    }

    public static RequestUtil getInstance(Context context) {
        synchronized (RequestUtil.class) {
            if (mInstance == null) {
                mInstance = new RequestUtil(context);
            }
        }
        return mInstance;
    }

    public void request(String url, final RequestCallBack callBack) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callBack != null) {
                    callBack.onError(e.getMessage());
                    LogUtil.e("RequestUtil post error " + e.getMessage());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String rsp = response.body().string();
                if (callBack != null && context instanceof Activity) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onSuccess(rsp);
                        }
                    });
                    //
                }
            }
        });
    }


    public void post(String url, Map<String, String> params, final RequestCallBack callBack) {
        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody.Builder builder = new FormBody.Builder();
        MediaType FORM_CONTENT_TYPE
                = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
        if (params != null && params.size() > 0) {
            Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> item = iterator.next();
                String key = item.getKey();
                String value = item.getValue();
                if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
                    builder.add(key, value);
                }
            }
        }
        RequestBody body = builder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callBack != null) {
                    callBack.onError(e.getMessage());
                    LogUtil.e("RequestUtil post error " + e.getMessage());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String rsp = response.body().string();
                if (callBack != null) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onSuccess(rsp);
                        }
                    });
                    //
                }
            }
        });
    }
}
